﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using yq.ControllerBase;
using System.Web;
using yq.SyncCaChe;
using System.Threading;

namespace yq.Controller
{
    public class Global : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //  filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //过滤这些扩展名的文件
            routes.IgnoreRoute("{*allgif}", new { allgif = @".*\.gif(/.*)?" });
            routes.IgnoreRoute("{*allcss}", new { allcss = @".*\.css(/.*)?" });
            routes.IgnoreRoute("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
            routes.IgnoreRoute("{*allpng}", new { allpng = @".*\.png(/.*)?" });
            routes.IgnoreRoute("{*alljs}", new { alljs = @".*\.js(/.*)?" });
            routes.IgnoreRoute("{*allhtml}", new { allhtml = @".*\.html(/.*)?" });
            routes.IgnoreRoute("{*allhtm}", new { allhtm = @".*\.htm(/.*)?" });

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                 null,
            new string[] { "yq.Controller.Home" }
            );

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();

            if (ex != null && WebConfig.IsDevelop)
            {
                //这里的错误一般是编译错误
                this.Context.Response.Write(ex.Message.ToArray(), 0, ex.Message.ToArray().Length);
                Server.ClearError();
            }
        }

        protected void Application_Start()
        {
            //注册验证类 
            VerifyHelp.Load();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //删除原引擎
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MyViewEngine());

            //好吧这个系统用不到定时任务
            //Sync sync = new Sync();
            //Thread td = new Thread(sync.Task);
            //td.Start();
        }
    }

    ///// <summary>
    ///// 异步任务
    ///// </summary>
    //internal class Sync
    //{
    //    /// <summary>
    //    /// 执行任务
    //    /// </summary>
    //    public void Task()
    //    {
    //        //延时启动
    //        Thread.Sleep(5000);
    //        cn.Util.HttpPost http = new cn.Util.HttpPost();
    //        http.Get("http://localhost/WebTeam/Task/Index");
    //        http.Get("http://localhost/WebTeam/Task/MonitorDB"); 
    //    }
    //}
}